/**
 * Copyright (c) 2018, dreamkaylee@foxmail.com All Rights Reserved.
 */

package com.common.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 字符串处理及转换工具类
 * @author limk
 * @date 2018年2月12日 下午6:19:05
 * @version 1.0
 */
public class StringUtil {
	
	private static Logger logger = LogManager.getLogger(StringUtil.class);

	private static Pattern numericPattern = Pattern.compile("^[0-9\\-]+$");
	private static Pattern numericStringPattern = Pattern.compile("^[0-9\\-]+$");
	private static Pattern floatNumericPattern = Pattern.compile("^[0-9\\-\\.]+$");
	private static Pattern abcPattern = Pattern.compile("^[a-z|A-Z]+$");
	public static final String SPLITSTRPATTERN = ",|，|;|；|、|\\.|。|-|_|\\(|\\)|\\[|\\]|\\{|\\}|\\\\|/| |　|\"";

	/**
	 * 判断是否是空字符串 null和""
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String s) {
		if (s != null && !s.equals("")) {
			return false;
		}
		return true;
	}
	
	/**
	 * 判断对象是否Empty
	 *       实用于对如下对象做判断:String Collection及其子类 Map及其子类
	 * @param pObj
	 * @return
	 */
	public static final boolean isEmpty(Object pObj) {
		if (pObj == null) {
			return true;
		}
		if (pObj == "") {
			return true;
		}
		if (pObj instanceof String) {
			if (((String) pObj).trim().length() == 0) {
				return true;
			}
		} else if (pObj instanceof Collection<?>) {
			if (((Collection<?>) pObj).size() == 0) {
				return true;
			}
		} else if (pObj instanceof Map<?, ?>) {
			if (((Map<?, ?>) pObj).size() == 0) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断对象是否为NotEmpty
	 *       实用于对如下对象做判断:String Collection及其子类 Map及其子类
	 * @param pObj
	 * @return
	 */
	public static final boolean isNotEmpty(Object pObj) {
		if (pObj == null) {
			return false;
		}
		if (pObj == "") {
			return false;
		}
		if (pObj instanceof String) {
			if (((String) pObj).trim().length() == 0) {
				return false;
			}
		} else if (pObj instanceof Collection<?>) {
			if (((Collection<?>) pObj).size() == 0) {
				return false;
			}
		} else if (pObj instanceof Map<?, ?>) {
			if (((Map<?, ?>) pObj).size() == 0) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 判断一个字符串是否都为数字
	 * @param strNum
	 * @return
	 */
	public boolean isDigit(String strNum) {
		String regex = "[0-9]+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher((CharSequence) strNum);
		return matcher.matches();
	}
	
	/**
	 * 判断是否数字表示
	 * @param src 源字符串
	 * @return
	 */
	public static boolean isNumeric(String src) {
		boolean value = false;
		if (src != null && src.length() > 0) {
			Matcher m = numericPattern.matcher(src);
			if (m.find()) {
				value = true;
			}
		}
		return value;
	}

	/**
	 * 判断是否数字表示
	 * @param src
	 * @return
	 */
	public static boolean isNumericString(String src) {
		boolean value = false;
		if (src != null && src.length() > 0) {
			Matcher m = numericStringPattern.matcher(src);
			if (m.find()) {
				value = true;
			}
		}
		return value;
	}

	/**
	 * 判断是否浮点数字表示
	 * @param src
	 * @return
	 */
	public static boolean isFloatNumeric(String src) {
		boolean value = false;
		if (src != null && src.length() > 0) {
			Matcher m = floatNumericPattern.matcher(src);
			if (m.find()) {
				value = true;
			}
		}
		return value;
	}
	
	/**
	 * 字符串转int
	 * @param s
	 * @return
	 */
	public static int toInt(String s) {
		if (s != null && !"".equals(s.trim())) {
			try {
				return Integer.parseInt(s);
			} catch (NumberFormatException e) {
				return 0;
			}
		}
		return 0;
	}

	/**
	 * 字符串转double
	 * @param s
	 * @return
	 */
	public static double toDouble(String s) {
		if (s != null && !"".equals(s.trim())) {
			try {
				return Double.parseDouble(s);
			} catch (NumberFormatException e) {
				return new Float(0);
			}
		}
		return 0;
	}
	
	/**
	 * 字符串转float
	 * @param s
	 * @return
	 */
	public static float toFloat(String s) {
		if (s != null && !"".equals(s.trim())) {
			try {
				return Float.parseFloat(s);
			} catch (NumberFormatException e) {
				return new Float(0);
			}
		}
		return 0;
	}
	
	/**
	 * 字符串转long
	 * @param s
	 * @return
	 */
	public static long toLong(String s) {
		if (s != null && !"".equals(s.trim())) {
			try {
				return Long.parseLong(s);
			} catch (NumberFormatException exception) {
				return new Long(0);
			}
		}
		return 0L;
	}
	
	/**
	 * 字符串转BigDecimal
	 * @param s
	 * @return
	 */
	public static BigDecimal toBigDecimal(String s) {
		return BigDecimal.valueOf(toDouble(s));
	}
	
	/**
	 * 数字转字符串,如果num<=0 则输出""
	 * @param num
	 * @return
	 */
	public static String numberToString(Object num) {
		if (num == null) {
			return null;
		} else if (num instanceof Integer && (Integer) num > 0) {
			return Integer.toString((Integer) num);
		} else if (num instanceof Long && (Long) num > 0) {
			return Long.toString((Long) num);
		} else if (num instanceof Float && (Float) num > 0) {
			return Float.toString((Float) num);
		} else if (num instanceof Double && (Double) num > 0) {
			return Double.toString((Double) num);
		} else if (num instanceof Short && (Short) num > 0) {
			return Short.toString((Short) num);
		} else if (num instanceof Byte && (Byte) num > 0) {
			return Byte.toString((Byte) num);
		} else {
			return "";
		}
	}
	
	/**
	 * 货币转字符串
	 * @param money
	 * @param style 样式 [default]要格式化成的格式如： #.00, #.#
	 * @return
	 */
	public static String moneyToString(Object money, String style) {
		String defaults = "default";
		int ten = 10;
		boolean is = money != null && style != null && (money instanceof Double || money instanceof Float);
		if (is) {
			Double num = (Double) money;
			if (style.equalsIgnoreCase(defaults)) {
				// 缺省样式 0 不输出 ,如果没有输出小数位则不输出.0
				if (num == 0) {
					// 不输出0
					return "";
				} else if ((num * ten % ten) == 0) {
					// 没有小数
					return Integer.toString((int) num.intValue());
				} else {
					// 有小数
					return num.toString();
				}
			} else {
				DecimalFormat df = new DecimalFormat(style);
				return df.format(num);
			}
		}
		return null;
	}
	
	/**
	 * 判断是否纯字母组合
	 * @param src
	 * @return
	 */
	public static boolean isABC(String src) {
		boolean value = false;
		if (src != null && src.length() > 0) {
			Matcher m = abcPattern.matcher(src);
			if (m.find()) {
				value = true;
			}
		}
		return value;
	}

	/**
	 * 把List中的每个元素用给定的符号symbol连接成一个字符串
	 * @param array
	 * @param symbol
	 * @return
	 */
	public static String joinString(List<?> array, String symbol) {
		String result = "";
		if (array != null) {
			for (int i = 0; i < array.size(); i++) {
				String temp = array.get(i).toString();
				if (temp != null && temp.trim().length() > 0) {
					result += (temp + symbol);
				}
			}
			if (result.length() > 1) {
				result = result.substring(0, result.length() - 1);
			}
		}
		return result;
	}
	
	/**
	 * 把array中的每个元素用给定的符号symbol连接成一个字符串
	 * @param array
	 * @param symbol
	 * @return
	 */
	public static String joinString(String[] array, String symbol) {
		String result = "";
		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				String temp = array[i];
				if (temp != null && temp.trim().length() > 0) {
					result += (temp + symbol);
				}
			}
			if (result.length() > 1) {
				result = result.substring(0, result.length() - 1);
			}
		}
		return result;
	}
	
	/**
	 * 字符串超出规定长度自动省略
	 * @param subject
	 * @param size
	 * @return
	 */
	public static String subStringNotEncode(String subject, int size) {
		if (subject != null && subject.length() > size) {
			subject = subject.substring(0, size) + "...";
		}
		return subject;
	}
	
	/**
	 * 截取字符串　超出的字符用symbol代替 
	 * @param str
	 * @param len 字符串长度　长度计量单位为一个GBK汉字　　两个英文字母计算为一个单位长度
	 * @param symbol
	 * @return
	 */
	public static String getLimitLengthString(String str, int len, String symbol) {
		int iLen = len * 2;
		int counterOfDoubleByte = 0;
		int two = 2;
		String strRet = "";
		try {
			if (str != null) {
				byte[] b = str.getBytes("GBK");
				if (b.length <= iLen) {
					return str;
				}
				for (int i = 0; i < iLen; i++) {
					if (b[i] < 0) {
						counterOfDoubleByte++;
					}
				}
				if (counterOfDoubleByte % two == 0) {
					strRet = new String(b, 0, iLen, "GBK") + symbol;
					return strRet;
				} else {
					strRet = new String(b, 0, iLen - 1, "GBK") + symbol;
					return strRet;
				}
			} else {
				return "";
			}
		} catch (Exception ex) {
			return str.substring(0, len);
		} finally {
			strRet = null;
		}
	}

	/**
	 * 截取字符串　超出的字符用symbol代替 
	 * @param str
	 * @param len 字符串长度　长度计量单位为一个GBK汉字　　两个英文字母计算为一个单位长度
	 * @return
	 */
	public static String getLimitLengthString(String str, int len) {
		return getLimitLengthString(str, len, "...");
	}

	/**
	 * 截取字符，不转码
	 * @param subject
	 * @param size
	 * @return
	 */
	public static String subStrNotEncode(String subject, int size) {
		if (subject.length() > size) {
			subject = subject.substring(0, size);
		}
		return subject;
	}

	/**
	 * 取得字符串的实际长度（包含汉字的情况）
	 * @param str
	 * @return
	 */
	public static int getStringLen(String str) {
		int value = 0;
		if (str != null) {
			char[] theChars = str.toCharArray();
			for (int i = 0; i < theChars.length; i++) {
				value += (theChars[i] <= 255) ? 1 : 2;
			}
		}
		return value;
	}

	/**
	 * 检查数据串中是否包含非法字符集
	 * @param str
	 * @return
	 */
	public static boolean check(String str) {
		int len = SPLITSTRPATTERN.length();
		if (null == str) {
			return false;
		}
		for (int i = 0; i < len; i++) {
			if (str.indexOf(SPLITSTRPATTERN.charAt(i)) != -1) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 去除字符串中的空格、回车、换行符、制表符
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {
		if (str != null) {
			String regex = "\\s*|\t|\r|\n";
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(str);
			str = m.replaceAll("");
		}
		return str;
	}
	
	/**
	 * 判断sou中是否包含finds
	 * @param sou
	 * @param finds
	 * @return
	 */
	public static boolean strPos(String sou, String... finds) {
		if (sou != null && finds != null && finds.length > 0) {
			for (int i = 0; i < finds.length; i++) {
				if (sou.indexOf(finds[i]) > -1) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 判断sou中是否包含finds
	 * @param sou
	 * @param finds
	 * @return
	 */
	public static boolean strPos(String sou, List<String> finds) {
		if (sou != null && finds != null && finds.size() > 0) {
			for (String s : finds) {
				if (sou.indexOf(s) > -1) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 判断sou中是否包含finds
	 * @param sou
	 * @param finds
	 * @return
	 */
	public static boolean strPos(String sou, String finds) {
		List<String> t = splitToList(",", finds);
		return strPos(sou, t);
	}
	
	/**
	 * 判断某个字符串是否存在于数组中
	 * @param stringArray 原数组
	 * @param source 查找的字符串
	 * @return
	 */
	public static boolean contains(String[] stringArray, String source) {
		// 转换为list
		List<String> tempList = Arrays.asList(stringArray);

		// 利用list的包含方法,进行判断
		if (tempList.contains(source)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 自定义的分隔字符串函数
	 *       例如: 1,2,3 =>[1,2,3] 3个元素 ,2,3=>[,2,3] 3个元素 ,2,3,=>[,2,3,] 4个元素 ,,,=>[,,,] 4个元素
	 *       为提高速度不用正则表达式 两个间隔符,,返回""元素
	 * @param split 分割字符  默认","
	 * @param src 输入字符串
	 * @return 分隔后的list
	 */
	public static List<String> splitToList(String split, String src) {
		String sp = ",";
		if (split != null && split.length() == 1) {
			sp = split;
		}
		List<String> r = new ArrayList<String>();
		int lastIndex = -1;
		int index = src.indexOf(sp);
		if (-1 == index && src != null) {
			r.add(src);
			return r;
		}
		while (index >= 0) {
			if (index > lastIndex) {
				r.add(src.substring(lastIndex + 1, index));
			} else {
				r.add("");
			}

			lastIndex = index;
			index = src.indexOf(sp, index + 1);
			if (index == -1) {
				r.add(src.substring(lastIndex + 1, src.length()));
			}
		}
		return r;
	}
	
	/**
	 * 判断两个字符串是否相等
	 *       1.如果都为null则判断为相等
     *       2.一个为null另一个not null则判断不相等
     *       3.如果s1=s2则相等
	 * @param s1
	 * @param s2
	 * @return
	 */
	public static boolean equals(String s1, String s2) {
		if (StringUtil.isEmpty(s1) && StringUtil.isEmpty(s2)) {
			return true;
		} else if (!StringUtil.isEmpty(s1) && !StringUtil.isEmpty(s2)) {
			return s1.equals(s2);
		}
		return false;
	}
	
	/**
	 * 隐藏邮件地址前缀
	 * @param email
	 * @return
	 */
	public static String getHideEmailPrefix(String email) {
		if (null != email) {
			int index = email.lastIndexOf('@');
			if (index > 0) {
				email = repeat("*", index).concat(email.substring(index));
			}
		}
		return email;
	}

	/**
	 * 通过源字符串重复生成N次组成新的字符串
	 * @param src
	 * @param num
	 * @return
	 */
	public static String repeat(String src, int num) {
		StringBuffer s = new StringBuffer();
		for (int i = 0; i < num; i++) {
			s.append(src);
		}
		return s.toString();
	}
	
	/**
	 * 根据指定的字符把源字符串分割成一个数组
	 * @param pattern
	 * @param src
	 * @return
	 */
	public static List<String> parseString2ListByCustomerPattern(String pattern, String src) {
		if (src == null) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		String[] result = src.split(pattern);
		for (int i = 0; i < result.length; i++) {
			list.add(result[i]);
		}
		return list;
	}
	
	/**
	 * 格式化一个float
	 * @param f
	 * @param format 要格式化成的格式 such as #.00, #.#
	 * @return
	 */
	public static String formatFloat(float f, String format) {
		DecimalFormat df = new DecimalFormat(format);
		return df.format(f);
	}

	/**
	 * 字符串替换
	 * @param str 源字符串
	 * @param sr 正则表达式样式
	 * @param sd 替换文本
	 * @return
	 */
	public static String stringReplace(String str, String sr, String sd) {
		String regEx = sr;
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		str = m.replaceAll(sd);
		return str;
	}
	
	/**
	 * 截取字符串
	 *       跳过jmp, 返回sb和se之间的字符串
	 * @param s
	 * @param jmp
	 * @param sb
	 * @param se
	 * @return
	 */
	public static String subStringExe(String s, String jmp, String sb, String se) {
		if (isEmpty(s)) {
			return "";
		}
		int i = s.indexOf(jmp);
		if (i >= 0 && i < s.length()) {
			s = s.substring(i + 1);
		}
		i = s.indexOf(sb);
		if (i >= 0 && i < s.length()) {
			s = s.substring(i + 1);
		}
		if (se == "") {
			return s;
		} else {
			i = s.indexOf(se);
			if (i >= 0 && i < s.length()) {
				s = s.substring(i + 1);
			}
			return s;
		}
	}
	
	/**
	 * 把list转换成以“,”相隔的字符串 调用时注意类型初始化（申明类型） 
	 * @param list
	 * @return
	 */
	public static <T> String listTtoString(List<T> list) {
		if (list == null || list.size() < 1) {
			return "";
		}
		Iterator<T> i = list.iterator();
		if (!i.hasNext()) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (;;) {
			T e = i.next();
			sb.append(e);
			if (!i.hasNext()) {
				return sb.toString();
			}
			sb.append(",");
		}
	}
	
	/**
	 * 把整形数组转换成以“,”相隔的字符串
	 * @param a
	 * @return
	 */
	public static String intArraytoString(int[] a) {
		if (a == null) {
			return "";
		}
		int iMax = a.length - 1;
		if (iMax == -1) {
			return "";
		}
		StringBuilder b = new StringBuilder();
		for (int i = 0;; i++) {
			b.append(a[i]);
			if (i == iMax) {
				return b.toString();
			}
			b.append(",");
		}
	}
	
	/**
	 * 获取从start开始用*替换len个长度后的字符串
	 * @param str 要替换的字符串
	 * @param start 开始位置
	 * @param len 长度
	 * @return
	 */
	public static String getMaskStr(String str, int start, int len) {
		if (StringUtil.isEmpty(str)) {
			return str;
		}
		if (str.length() < start) {
			return str;
		}

		// 获取*之前的字符串
		String ret = str.substring(0, start);

		// 获取最多能打的*个数
		int strLen = str.length();
		if (strLen < start + len) {
			len = strLen - start;
		}

		// 替换成*
		for (int i = 0; i < len; i++) {
			ret += "*";
		}

		// 加上*之后的字符串
		if (strLen > start + len) {
			ret += str.substring(start + len);
		}

		return ret;
	}
	
	/**
	 * 解析字符串返回map键值对
	 * @param query 源参数字符串
	 * @param split1 键值对之间的分隔符（例：&）
	 * @param split2 key与value之间的分隔符（例：=）
	 * @param dupLink 重复参数名的参数值之间的连接符，连接后的字符串作为该参数的参数值，可为null null：不允许重复参数名出现，则靠后的参数值会覆盖掉靠前的参数值。
	 * @return
	 */
	public static Map<String, String> parseQuery(String query, char split1, char split2, String dupLink) {
		if (!isEmpty(query) && query.indexOf(split2) > 0) {
			Map<String, String> result = new HashMap<String, String>(16);

			String name = null;
			String value = null;
			String tempValue = "";
			int len = query.length();
			for (int i = 0; i < len; i++) {
				char c = query.charAt(i);
				if (c == split2) {
					value = "";
				} else if (c == split1) {
					if (!isEmpty(name) && value != null) {
						if (dupLink != null) {
							tempValue = result.get(name);
							if (tempValue != null) {
								value += dupLink + tempValue;
							}
						}
						result.put(name, value);
					}
					name = null;
					value = null;
				} else if (value != null) {
					value += c;
				} else {
					name = (name != null) ? (name + c) : "" + c;
				}
			}

			if (!isEmpty(name) && value != null) {
				if (dupLink != null) {
					tempValue = result.get(name);
					if (tempValue != null) {
						value += dupLink + tempValue;
					}
				}
				result.put(name, value);
			}

			return result;
		}
		return null;
	}
	
	/**
	 * 将list 用传入的分隔符组装为String
	 * @param list
	 * @param slipStr
	 * @return
	 */
	public static String listToStringSlipStr(List<?> list, String slipStr) {
		StringBuffer returnStr = new StringBuffer();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				returnStr.append(list.get(i)).append(slipStr);
			}
		}
		if (returnStr.toString().length() > 0) {
			return returnStr.toString().substring(0, returnStr.toString().lastIndexOf(slipStr));
		} else {
			return "";
		}
	}
	
	/**
	 * 截取字符串，并添加自定义后缀
	 * @param str 原始字符串
	 * @param len 截取的长度
	 * @param tail 结束加上的后缀
	 * @return
	 */
	public static String getHtmlSubString(String str, int len, String tail) {
		if (str == null || str.length() <= len) {
			return str;
		}
		int length = str.length();
		char c = ' ';
		String tag = null;
		String name = null;
		int size = 0;
		String result = "";
		boolean isTag = false;
		List<String> tags = new ArrayList<String>();
		int i = 0;
		for (int end = 0, spanEnd = 0; i < length && len > 0; i++) {
			c = str.charAt(i);
			if (c == '<') {
				end = str.indexOf('>', i);
			}

			if (end > 0) {
				// 截取标签
				tag = str.substring(i, end + 1);
				int n = tag.length();
				if (tag.endsWith("/>")) {
					isTag = true;
				// 结束符
				} else if (tag.startsWith("</")) { 
					name = tag.substring(2, end - i);
					size = tags.size() - 1;
					// 堆栈取出html开始标签
					if (size >= 0 && name.equals(tags.get(size))) {
						isTag = true;
						tags.remove(size);
					}
				} else { // 开始符
					spanEnd = tag.indexOf(' ', 0);
					spanEnd = spanEnd > 0 ? spanEnd : n;
					name = tag.substring(1, spanEnd);
					if (name.trim().length() > 0) {
						// 如果有结束符则为html标签
						spanEnd = str.indexOf("</" + name + ">", end);
						if (spanEnd > 0) {
							isTag = true;
							tags.add(name);
						}
					}
				}
				// 非html标签字符
				if (!isTag) {
					if (n >= len) {
						result += tag.substring(0, len);
						break;
					} else {
						len -= n;
					}
				}

				result += tag;
				isTag = false;
				i = end;
				end = 0;
			} else { // 非html标签字符
				len--;
				result += c;
			}
		}
		// 添加未结束的html标签
		for (String endTag : tags) {
			result += "</" + endTag + ">";
		}
		if (i < length) {
			result += tail;
		}
		return result;
	}
	
	/**
	 * 根据传入的分割符号,把传入的字符串分割为List字符串
	 * @param slipStr 分隔的字符
	 * @param src
	 * @return
	 */
	public static List<String> stringToStringListBySlipStr(String slipStr, String src) {
		if (src == null) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		String[] result = src.split(slipStr);
		for (int i = 0; i < result.length; i++) {
			list.add(result[i]);
		}
		return list;
	}
	
	/**
	 * 把 名=值 参数表转换成字符串
	 * @param map
	 * @return
	 */
	public static String linkedHashMapToString(LinkedHashMap<String, String> map) {
		if (map != null && map.size() > 0) {
			String result = "";
			Iterator<?> it = map.keySet().iterator();
			while (it.hasNext()) {
				String name = (String) it.next();
				String value = (String) map.get(name);
				result += (result.equals("")) ? "" : "&";
				result += String.format("%s=%s", name, value);
			}
			return result;
		}
		return null;
	}

	/**
	 * 解析字符串返回 名称=值的参数表
	 * @param str
	 * @return
	 */
	public static LinkedHashMap<String, String> toLinkedHashMap(String str) {
		String equal = "=";
		if (str != null && !str.equals("") && str.indexOf(equal) > 0) {
			LinkedHashMap<String, String> result = new LinkedHashMap<String, String>();

			String name = null;
			String value = null;
			int i = 0;
			while (i < str.length()) {
				char c = str.charAt(i);
				switch (c) {
				// =
				case 61: 
					value = "";
					break;
				// &
				case 38:
					if (name != null && value != null && !name.equals("")) {
						result.put(name, value);
					}
					name = null;
					value = null;
					break;
				default:
					if (value != null) {
						value = (value != null) ? (value + c) : "" + c;
					} else {
						name = (name != null) ? (name + c) : "" + c;
					}
				}
				i++;

			}

			if (name != null && value != null && !name.equals("")) {
				result.put(name, value);
			}

			return result;

		}
		return null;
	}
	
	/**
	 * 截取数字
	 * @param content
	 * @return
	 */
	public String getNumbers(String content) {
		String regex = "\\d+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			return matcher.group(0);
		}
		return "";
	}

	/**
	 * 截取非数字
	 * @param content
	 * @return
	 */
	public String splitNotNumber(String content) {
		String regex = "\\D+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			return matcher.group(0);
		}
		return "";
	}
		
	/**
	 * 首字母大写
	 * @param realName
	 * @return
	 */
	public static String firstUpperCase(String realName) {
		return StringUtils.replaceChars(realName, realName.substring(0, 1),realName.substring(0, 1).toUpperCase());
	}

	/**
	 * 首字母小写
	 * @param realName
	 * @return
	 */
	public static String firstLowerCase(String realName) {
		return StringUtils.replaceChars(realName, realName.substring(0, 1),realName.substring(0, 1).toLowerCase());
	}
	
	/**
	 * 转换编码
	 * @param s 源字符串
	 * @param fencode 源编码格式
	 * @param bencode 目标编码格式
	 * @return 目标编码
	 */
	public static String changCoding(String s, String fencode, String bencode) {
		String str;
		try {
			if (StringUtil.isNotEmpty(s)) {
				str = new String(s.getBytes(fencode), bencode);
			} else {
				str = "";
			}
			return str;
		} catch (UnsupportedEncodingException e) {
			return s;
		}
	}
	
	/**
	 * 全角生成半角
	 * @param qJstr
	 * @return
	 */
	public static String q2B(String qJstr) {
		String outStr = "";
		String tStr = "";
		byte[] b = null;
		for (int i = 0; i < qJstr.length(); i++) {
			try {
				tStr = qJstr.substring(i, i + 1);
				b = tStr.getBytes("unicode");
			} catch (java.io.UnsupportedEncodingException e) {
				if (logger.isErrorEnabled()) {
					logger.error(e);
				}
			}
			if (b[3] == -1) {
				b[2] = (byte) (b[2] + 32);
				b[3] = 0;
				try {
					outStr = outStr + new String(b, "unicode");
				} catch (java.io.UnsupportedEncodingException ex) {
					if (logger.isErrorEnabled()) {
						logger.error(ex);
					}
				}
			} else {
				outStr = outStr + tStr;
			}
		}
		return outStr;
	}
	
	/**
	 * 全角字符变半角字符
	 * @param str
	 * @return
	 */
	public static String full2Half(String str) {
		if (str == null || "".equals(str)) {
			return "";
		}
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);

			if (c >= 65281 && c < 65373) {
				sb.append((char) (c - 65248));
			} else {
				sb.append(str.charAt(i));
			}
		}

		return sb.toString();

	}

	/**
	 * 全角括号转为半角
	 * @param str
	 * @return
	 */
	public static String replaceBracketStr(String str) {
		if (str != null && str.length() > 0) {
			str = str.replaceAll("（", "(");
			str = str.replaceAll("）", ")");
		}
		return str;
	}
	
}
